package com.example.filelock.utils;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class FileZipUtil {

    /**
     * 压缩文件并存放到指定目录
     * @param fileFrom  选择压缩的文件路径
     * @param fileTo    选择压缩后的文件路径
     */
    public static void zipFile(String fileFrom, String fileTo) {

        try {
            FileInputStream in = new FileInputStream(fileFrom);
            FileOutputStream out = new FileOutputStream(fileTo);
            ZipOutputStream zipOut = new ZipOutputStream(out);
            ZipEntry entry = new ZipEntry(new File(fileFrom).getName());
            zipOut.putNextEntry(entry);
            int nNumber;
            byte[] buffer = new byte[512];
            while ((nNumber = in.read(buffer)) != -1){
                zipOut.write(buffer, 0, nNumber);
            }
            zipOut.close();

            out.close();
            in.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    /**
     * 解压文件
     * @param zipFile   解压的文件路径
     * @param targetDir 输入解压的文件夹路径
     */
    public static void Unzip(String zipFile, String targetDir) {
        int BUFFER = 4096; //这里缓冲区我们使用4KB，
        String strEntry; //保存每个zip的条目名称
        try {
            BufferedOutputStream dest = null; //缓冲输出流
            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry; //每个zip条目的实例
            while ((entry = zis.getNextEntry()) != null) {
                try {
                    int count;
                    byte data[] = new byte[BUFFER];
                    strEntry = entry.getName();
                    File entryFile = new File(targetDir +"/t-out_"+ strEntry);
                    File entryDir = new File(entryFile.getParent());
                    if (!entryDir.exists()) {
                        entryDir.mkdirs();
                    }
                    FileOutputStream fos = new FileOutputStream(entryFile);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {

                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            zis.close();
        } catch (Exception cwj) {
            cwj.printStackTrace();
        }
    }
    //密码加密
    public  static String setFilePwd(String pwd){
        StringBuilder vs = new StringBuilder();
        try {
            for (int i = 0; i < pwd.length(); i++) {
                vs.append((char) (pwd.charAt(i) + '0'-'a'));
            }
        }catch (Exception e){
            Log.d(BasDataUtil.LOG_TOAST,"错误:"+e.toString());
        }

        return  vs.toString();
    }
    //密码解密
    public  static String getFilePwd(String pwd){
        StringBuilder vs = new StringBuilder();
        StringBuffer returns = new StringBuffer();
        try {
            for (int i = 0; i < pwd.length(); i++) {
                vs.append((char) (pwd.charAt(i) - '0'+'a'));
            }
//        排除

            for (int i = 0; i <vs.length() ; i++) {
                if (vs.charAt(i)>='0'&&vs.charAt(i)<='9'){
                    returns.append(vs.charAt(i));
                }
            }
        }catch (Exception e){
            Log.d(BasDataUtil.LOG_TOAST,"错误:"+e.toString());
        }
        return  returns.toString();
    }
}
